﻿using Microsoft.Practices.Prism.Commands;
using Sinergija13.DataServices.Store.App;
using Sinergija13.DataServices.Store.Twitter;
using Sinergija13.DataServices.Web.Twitter;
using Sinergija13.WP7App.Services;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

namespace Sinergija13.WP7App.ViewModels
{
    public class TwitterSettingsViewModel : ViewModel
    {
        private readonly IAppSettingsStoreLocator appSettingsStoreLocator;
        private readonly ITwitterDataStoreLocator twitterDataStoreLocator;
        private TweetType previousTweetType;
        private int currentSelectedIndex = 0;
        private bool tweetsDeleted = false;

        public TwitterSettingsViewModel(
            IAppSettingsStoreLocator appSettingsStoreLocator,
            ITwitterDataStoreLocator twitterDataStoreLocator,
            INavigationService navigationService)
            : base(navigationService)
        {
            this.appSettingsStoreLocator = appSettingsStoreLocator;
            this.twitterDataStoreLocator = twitterDataStoreLocator;
            initialize();
        }

        private void initialize()
        {
            this.SelectionChangedCommand = new DelegateCommand(
            () =>
            {
                TweetType newTweetType = CurrentSelectedIndex == 0 ?
                    TweetType.User :
                    (CurrentSelectedIndex == 1 ? TweetType.Hashtag : TweetType.UserAndHashtag);
                
                appSettingsStoreLocator.GetStore().TweetType = newTweetType;
            });

            this.SaveCommand = new DelegateCommand(
            () =>
            {
                this.NavigationService.GoBack();
            });

            this.ClearTwitterDataStoreCommand = new DelegateCommand(
                () =>
                {
                    twitterDataStoreLocator.GetStore().ClearStore();
                    TweetsDeleted = true;
                });

            this.CancelCommand = new DelegateCommand(
            () =>
            {
                appSettingsStoreLocator.GetStore().TweetType = previousTweetType;
                this.NavigationService.GoBack();
            });

            previousTweetType = appSettingsStoreLocator.GetStore().TweetType;

            CurrentSelectedIndex = previousTweetType == TweetType.User ?
                0 :
                (previousTweetType == TweetType.Hashtag ? 1 : 2);

            IsBeingActivated();
        }

        public DelegateCommand SelectionChangedCommand { get; set; }
        public DelegateCommand ClearTwitterDataStoreCommand { get; set; }
        public DelegateCommand SaveCommand { get; set; }
        public DelegateCommand CancelCommand { get; set; }

        public List<string> TweetTypeOptions
        {
            get
            {
                var list = new List<string>();
                list.Add("@mssinergija");
                list.Add("#mssinergija");
                list.Add("@mssinergija + #mssinergija");

                return list;
            }
        }

        public bool TweetsDeleted
        {
            get
            {
                return tweetsDeleted;
            }
            set
            {
                tweetsDeleted = value;
                RaisePropertyChanged(() => TweetsDeleted);
            }
        }

        public string OptionDescription
        {
            get
            {
                switch (CurrentSelectedIndex)
                {
                    case 0:
                        return "Twitter feed će dohvatati samo tweet-ove zvaničnog Microsoft Sinergija Twitter naloga.";
                    case 1:
                        return "Twitter feed će dohvatati samo tweet-ove koji sadrže #mssinergija hashtag.";
                    case 2:
                        return "Twitter feed će dohvatati i tweet-ove zvaničnog Microsoft Sinergija Twitter naloga i tweet-ove koji sadrže #mssinergija hashtag.";
                }

                return "";
            }
        }

        public int CurrentSelectedIndex
        {
            get
            {
                return this.currentSelectedIndex;
            }
            set
            {
                this.currentSelectedIndex = value;
                RaisePropertyChanged(() => CurrentSelectedIndex);
                RaisePropertyChanged(() => OptionDescription);
            }
        }

        public override void IsBeingActivated()
        {}
    }
}
